package com.byzk.p2p.admin.system.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.byzk.p2p.admin.system.dao.ResourceDao;
import com.byzk.p2p.admin.system.dto.Resource;
import com.byzk.p2p.admin.system.vo.ResourceVo;

@Service
public class ResourceService {

	@Autowired
	private ResourceDao resourceDao;
	
	public Integer saveToResource(String path,String filename,String ip,String userid,String terminal){
		Resource resource=new Resource();
		resource.setCreateTime(new Date());
		resource.setCreateIp(ip);
		resource.setIsDelete("N");
		resource.setUserId(userid);
		//后缀名作为上传文件的类型
		String type=filename.substring(filename.lastIndexOf(".")+1, filename.length());
		resource.setType(type);
		resource.setPath(path);
		resource.setTerminal(terminal);
		resourceDao.insert(resource);
		return resource.getId();
	}
	
	/**
	 * 删除资源文件
	 * @param resourceId 资源文件id
	 * @return
	 */
	public Integer deleteResource(Integer resourceId){
		return resourceDao.delete(resourceId);
	}
	
	public Resource getResourceById(Integer resourceId){
		return resourceDao.getResourceById(resourceId);
	}
	
	public List<ResourceVo> getResourcesById(String ids){
		List<Integer> idsList=new ArrayList<Integer>();
		String[] list=ids.split(",");
		int i=0;
		if(list.length>0){
			for (String id : list) {
				idsList.add(i, Integer.parseInt(id));
				i++;
			}			
		}else{
			idsList.add(Integer.parseInt(ids), null);
		}
		return resourceDao.getResourcesById(idsList);
	}
}
